package com.rtwo.test;


import org.hibernate.annotations.Synchronize;

/**
 * Created by hasee on 2017/5/20.
 */
public class DrawThread extends Thread{
    private Account account;
    private double drawAmount;
    public DrawThread(String name,Account account,double drawAmount){
        super(name);
        this.account = account;
        this.drawAmount = drawAmount;
    }
    public void run(){
            if(account.get() >= drawAmount){
                System.out.println(getName()+"取钱成功!吐出钞票:"+drawAmount);
                try{
                    Thread.sleep(1);
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                account.set(account.get()-drawAmount);
                System.out.println("\t余额为:"+account.get());
            }
            else{
                System.out.println(this.getName()+"取钱失败!余额不足!");
            }
    }
    public static void main(String args[]){
        Account acct = new Account();
        new DrawThread("Tom",acct,800).start();
        new DrawThread("Jack",acct,800).start();
    }
}
class Account {
    private static ThreadLocal<Double> balance = new ThreadLocal<Double>(){
        public Double initialValue() {
            return new Double(1000);
        }
    };

    public void set(double i){
        balance.set(i);
    }

    public double get(){
        return balance.get();
    }

}
